
def solve(seats, neighbors):
    neighbor_count = {}
    change = True

    while change:
        change = False

        for seat in seats.keys():
            neighbor_count[seat] = sum(seats[n] == "#" for n in neighbors[seat])

        for (c, seat) in seats.items():
            if seat == "L":
                if neighbor_count[c] == 0:
                    seats[c] = "#"
                    change = True
            elif seat == "#":
                if neighbor_count[c] >= 4:
                    seats[c] = "L"
                    change = True

    return sum(seat == "#" for seat in seats.values())


with open('input.txt') as file:
    grid = [list(line.strip()) for line in file.readlines()]
    seats = {}
    neighbors = {}

    for (y, line) in enumerate(grid):
        for (x, seat) in enumerate(line):
            if seat == '.':
                continue
            
            seats[(x, y)] = seat
            curr_neighbors = []

            for (dx, dy) in [(dx, dy) for dx in (-1,0,1) for dy in (-1,0,1)]:
                if dx == 0 and dy == 0:
                    continue
                    
                nx = x + dx
                ny = y + dy

                if 0 <= ny < len(grid) and 0 <= nx < len(grid[0]):
                    if grid[ny][nx] == 'L':
                        curr_neighbors.append((nx, ny))

            neighbors[(x, y)] = curr_neighbors
                
    print(solve(seats, neighbors))